<?php

class Core_Component_System_Method_Request {

    function templateResultHelper($data = array(), $filename = '*/*/*') {
        $r = $GLOBALS[RC]->getRouter();
        if (!file_exists($filename)) {
            $arr = explode('/', $filename);
            if (count($arr) > 0) {
                $arr[0] = $arr[0] == '*' ? $r->getComponent() : $arr[0];
            }
            if (count($arr) > 1) {
                $arr[1] = $arr[1] == '*' ? $r->getController() : $arr[1];
            }
            if (count($arr) > 2) {
                $arr[2] = $arr[2] == '*' ? $r->getAction() : $arr[2];
            }
            $filename = 'component' . DS . str_replace('_', DS, str_replace('-', '_', implode(DS, $arr)));
        }
        return Soul_Loader::loadInstance('Soul_Request_Result_Template')
                ->setFile($filename)
                ->addData($data);
    }

    function setLocationHelper() {
        $args = func_get_args();
        foreach ($args as $str) {
            if ($str[0] == '{' && substr($str, -1) == '}') {
                $str = $GLOBALS[RC]->getLanguage()->__(substr($str, 1, -1));
            }
            $GLOBALS[RC]->getLocation()->append($str);
        }
    }

    function historyResultHelper($url) {
        return Soul_Loader::loadInstance('Request_Result_History')->setStartUrl($url);
    }

    function getHistoryHelper($startUrl) {
        return $GLOBALS[RC]->getRouter()->findHistory($startUrl);
    }

    function modelFormResultHelper($model, $dataSources = array(), $filename = '*/*/*') {
        $form = App::getBusiness('system/dms_form')
                ->setDataSources($dataSources)
                ->setModel($model)
                ->buildForm();
        return $GLOBALS[RC]->getHelper()->templateResult(array('data_sources' => $dataSources, 'model' => $model, 'model_form' => $form), $filename);
    }

}